aDataSet = [];
deleteImg = '<img id="delete" src="../images/icons/cross.png" />';
updateImg = '<img id="update" src="../images/icons/edit.png" />';
resultImg = '<img id="update" src="../images/icons/result.png" />';

$(document).ready(function(){
	
	var txtPrenomCandidat = $("#txtPrenomCandidat");
	var txtNomCandidat = $("#txtNomCandidat");
	var txtEmailCandidat = $("#txtEmailCandidat");
	var txtTelephoneCandidat = $("#txtTelephoneCandidat");
	var txtIdCandidat = $("#txtIdCandidat");
	var allFields= $([]).add(txtPrenomCandidat).add(txtNomCandidat).add(txtEmailCandidat).add(txtTelephoneCandidat).add(txtIdCandidat);

	function openDialog(idCandidat, nomCandidat, prenomCandidat, emailCandidat, telephoneCandidat){
		txtPrenomCandidat.val( txtPrenomCandidat.val() + prenomCandidat );
		txtNomCandidat.val( txtNomCandidat.val() + nomCandidat );
		txtEmailCandidat.val( txtEmailCandidat.val() + emailCandidat );
		txtTelephoneCandidat.val( txtTelephoneCandidat.val() + telephoneCandidat );
		txtIdCandidat.val(txtIdCandidat.val() + idCandidat);
		$('#dialog').dialog('open');
	}
	
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 450,
		modal: true,
		buttons: {
			'Modifier Candidat': function() {
				var candidat = {
						idCandidat : txtIdCandidat.val(),
						prenomCandidat: txtPrenomCandidat.val(),
						nomCandidat:txtNomCandidat.val(),
						emailCandidat:txtEmailCandidat.val(),
						telephoneCandidat:txtTelephoneCandidat.val()
			        };
				$.ajax({
					type : 'GET',
					url : '/qRH-WS/services/candidat/update',
					data : candidat,
					dataType : 'json',
					success : function(result, textStatus, jqXHR) {
						console.log('Success');
						location.reload();
					}
				});
				$(this).dialog('close');
			},
			Cancel: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
			allFields.val('').removeClass('ui-state-error');
		}
	});
	
	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			return true;
		}
	}
	
	function checkRegexp(o,regexp,n) {

		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			return true;
		}
	}
	
	$.ajax({
		type : 'Get',
		url : '/qRH-WS/services/candidat/getAll',
		async: false,
		dataType : 'json',
		crossDomain : true,
		success : function(result, textStatus, jqXHR) {
			jQuery.each(result, function() {
				aDataSet.push(eval([this.idCandidat, this.nomCandidat, this.prenomCandidat, this.emailCandidat, this.telephoneCandidat, resultImg, updateImg, deleteImg]));		 
			});   
		},
		error : function(xhr, ajaxOptions, thrownError) {
			alert(xhr.status);
			alert(xhr.responseText);
		}
	});

	$('#tableContainer').html( '<table class="display" id="dataTableCandidat"></table>' );
	
	$('#dataTableCandidat').dataTable( {
		"aaData": aDataSet,
		"aoColumns": [
			{ "sTitle": "ID" },
			{ "sTitle": "Nom" },
			{ "sTitle": "Prenom" },
			{ "sTitle": "Email" },
			{ "sTitle": "Telephone" },
			{ "sTitle": "Consulter resultat" },
			{ "sTitle": "Modifier" },
			{ "sTitle": "Supprimer" }
			]
	} );
	
	$('#dataTableCandidat tbody tr').each(
		function() {
			
	        var nTds = $('td', this);
	        var idCandidat = $(nTds[0]).text();
	        var nomCandidat = $(nTds[1]).text();
	        var prenomCandidat = $(nTds[2]).text();
	        var emailCandidat = $(nTds[3]).text();
	        var telephoneCandidat = $(nTds[4]).text();
	        
	        $('td', this).slice(-1).click(
        		function() {
		        	var answer = confirm("Voulez-vous vraiment supprimer ce candidat ?");
					if(answer) {
						$.ajax({
							type : 'Post',
							async: false,
							url : '/qRH-WS/services/candidat/delete',
							data : emailCandidat,
							dataType : 'json',
							success : location.reload()
						});
		        	};
		        }
	        );
	        
	        $('td', this).slice(-2,-1).click(
        		function() {
        			openDialog(idCandidat, nomCandidat, prenomCandidat, emailCandidat, telephoneCandidat);
		        }
	        );
	        
	        $('td', this).slice(-3,-2).click(
        		function() {
        			window.location.href = "consulterResultat.html?emailCandidat="+emailCandidat;
		        }
	        );
	    }
	);
	
	
});